package com.jsb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jsb.service.PurchaseOrderService;
import com.jsb.service.UserService;
import com.jsb.model.User;
import com.jsb.model.UserExample;
import com.jsb.model.PurchaseOrder;
import com.jsb.model.PurchaseOrderExample;
import com.jsb.common.utils.StringUtils;
import com.jsb.dto.UserPurchaseOrder;

@Controller
public class CardController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/card/list.do";
	
	@Resource
	private PurchaseOrderService purchaseOrderService;
	@Resource
	private UserService userService;
	
	@RequestMapping("/card/list")
	public String list(
			Model model,String phone,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		PurchaseOrderExample purchaseOrderExample = new PurchaseOrderExample();
		if(StringUtils.isNotEmpty(phone)){
			purchaseOrderExample.createCriteria().andPhoneNumLike("%"+phone+"%");
		}
		int totalCount = purchaseOrderService.countPurchaseOrder(purchaseOrderExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		purchaseOrderExample.setPageIndex((pageNum - 1) * pageSize);
		purchaseOrderExample.setPageSize(pageSize);
		purchaseOrderExample.setOrderByClause("paymentDate desc");
		List<PurchaseOrder> purchaseOrders = purchaseOrderService.getPagePurchaseOrder(purchaseOrderExample);
		List<UserPurchaseOrder> userPurchaseOrders = new ArrayList<>();
		//将invoice对象封装进purchaseorders中
		for (PurchaseOrder purchaseOrder : purchaseOrders) {
			//创建大对象
			UserPurchaseOrder userPurchaseOrder= new UserPurchaseOrder();
			//将小对象封装到大对象中
			userPurchaseOrder.setPurchaseOrder(purchaseOrder);
			
			String phoneNum1 = purchaseOrder.getPhoneNum();
			UserExample userExample= new UserExample();
			userExample.createCriteria().andPhoneNumberEqualTo(phoneNum1);
			//根据用户电话号码，查询出对象的invoice，结果也只能有一条
			List<User> users = userService.selectByExample(userExample);
			if(users !=null && users.size()>0){
				//将小对象封装到大对象中
				userPurchaseOrder.setUser(users.get(0));
			}
			
			userPurchaseOrders.add(userPurchaseOrder);
		}
		model.addAttribute("phone", phone);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("userPurchaseOrders", userPurchaseOrders);
		return "Card/list";
	}
//	@RequestMapping("/cardOut/list")
//	public String search(
//			Model model,
//			@RequestParam(value = "pageNum", required = false) Integer pageNum,
//			@RequestParam(value = "pageSize", required = false) Integer pageSize,
//			String phoneNum) {
//		int maxPageNum = 0;
//		if (pageSize == null) {
//			pageSize = defaultPageSize;
//		}
//		PurchaseOrderExample purchaseOrderExample = new PurchaseOrderExample();
//		//添加搜索条件
//		com.jsb.model.PurchaseOrderExample.Criteria purchaseOrderCriteria = purchaseOrderExample.createCriteria();
//		if(phoneNum !=null && !"".equals(phoneNum)){
//			purchaseOrderCriteria.andPhoneNumLike("%"+phoneNum+"%");
//		}
//		int totalCount = purchaseOrderService.countPurchaseOrder(purchaseOrderExample);
//		if(totalCount%pageSize == 0){
//			maxPageNum = totalCount/pageSize;
//		}else{
//			maxPageNum = totalCount/pageSize+1;
//		}
//		if (pageNum == null || pageNum < 1) {
//			pageNum = 1;
//		}else {
//			if(maxPageNum > 0 && pageNum > maxPageNum) {
//				pageNum = maxPageNum;
//			}
//		}
//		purchaseOrderExample.setPageIndex((pageNum - 1) * pageSize);
//		purchaseOrderExample.setPageSize(pageSize);
//		List<PurchaseOrder> purchaseOrders = purchaseOrderService.getPagePurchaseOrder(purchaseOrderExample);
//		//将invoice对象封装进purchaseorders中
//		for (PurchaseOrder purchaseOrder : purchaseOrders) {
//			String phoneNum1 = purchaseOrder.getPhoneNum();
//			InvoiceExample invoiceExample = new InvoiceExample();
//			Criteria createCriteria = invoiceExample.createCriteria();
//			createCriteria.andUserPhoneNumEqualTo(phoneNum1);
//			//根据用户电话号码，查询出对象的invoice，结果也只能有一条
//			List<Invoice> invoices = invoiceService.selectByExample(invoiceExample);
//			if(invoices !=null && invoices.size()>0){
//				purchaseOrder.setInvoice(invoices.get(0));
//			}
//		}
//		model.addAttribute("phoneNum", phoneNum);
//		model.addAttribute("totalCount", totalCount);
//		model.addAttribute("pageNum", pageNum);
//		model.addAttribute("pageSize", pageSize);
//		if(totalCount%pageSize == 0){
//			model.addAttribute("maxPageNum", totalCount/pageSize);
//		}else{
//			model.addAttribute("maxPageNum", totalCount/pageSize+1);
//		}
//		model.addAttribute("purchaseOrders", purchaseOrders);
//		return "/PurchaseOrder/list";
//	}
	

//	@RequestMapping("/purchaseOrder/del")
//	public String del(Model model,
//			@RequestParam(value = "uuid", required = true) String uuid) {
//		purchaseOrderService.delPurchaseOrderByUuid(uuid);
//		return redirect;
//	}
}
